using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 复活Buff - 死亡时自动复活
/// </summary>
public class ReviveBuff : BuffBase
{
    private bool m_HasRevived = false;
    private float m_ReviveHealthPercentage;
    private long m_ReviveHealthAmount;

    public ReviveBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is ReviveData reviveData)
        {
            m_ReviveHealthPercentage = reviveData.ReviveHealthPercentage;
            m_ReviveHealthAmount = reviveData.ReviveHealthAmount;
        }
        else
        {
            m_ReviveHealthPercentage = 0.5f; // 默认复活50%生命值
            m_ReviveHealthAmount = 0; // 不使用固定数值
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 播放复活Buff特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        Logger.Log($"{Target.name} 获得复活效果");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        // 检查目标是否死亡且未复活过
        if (Target.HealthComponent.IsDead && !m_HasRevived)
        {
            ReviveTarget();
        }
    }

    private void ReviveTarget()
    {
        m_HasRevived = true;
        
        // 计算复活生命值
        long reviveHealth;
        if (m_ReviveHealthAmount > 0)
        {
            reviveHealth = m_ReviveHealthAmount;
        }
        else
        {
            reviveHealth = (long)(Target.HealthComponent.MaxHealth * m_ReviveHealthPercentage);
        }
        
        // 复活目标
        Target.HealthComponent.TakeHeal(reviveHealth);
        
        // 播放复活特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状态
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("revived", true);
        }
        
        Logger.Log($"{Target.name} 复活了，恢复 {reviveHealth} 点生命值");
        
        // 触发复活事件
        var args = ReferencePool.Acquire<EntityReviveEventArgs>();
        args.Entity = Target;
        EventHelper.Fire(this, args);
        
        // 复活后移除复活Buff
        if (Target.BuffComponent != null)
        {
            Target.BuffComponent.RemoveBuff(BuffType.Revive);
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 复活效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复动画状态
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("revived", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Revive"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 复活效果不能叠加
    }
}